﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ClassLibrary.DAO;
using ClassLibrary.Entity;
namespace mprsc
{
    public partial class ActionList : Form
    {
        TblJobNoteDAO jobDAO = new TblJobNoteDAO(Global.CONNECTION_STRING);
        TblStatusJobDAO StatusJobDAO = new TblStatusJobDAO(Global.CONNECTION_STRING);

        public delegate void SendMessage(string Message);
        public SendMessage Sender; 
        public ActionList()
        {
            InitializeComponent();
            //Tao con tro toi hàm GetMessage
            Sender = new SendMessage(GetMessage);
           // loadJobNotes();
        }
        //Hàm có nhiem vu lay tham so truyen vào
        private void GetMessage(string Message)
        {
            if (grdActionList.RowCount > 1)
            {
                txtJobNoteId.Text = Message;
            }
            else
            {
                txtJobId.Text = Message;
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            this.validationProvider1.ValidationMessages(!this.validationProvider1.Validate()); 
            int id = Global.ConvertToInt(txtJobId.Text);
            int id1 = Global.ConvertToInt(txtJobNoteId.Text);

            bool flag = false;
            try
            {
                TblJobNotes jobnote = new TblJobNotes();
                jobnote.JobID = id;
                jobnote.Notes = txtNote.Text + " .ACTION BY " + txtActionBy.Text.Trim();

                jobnote.DateEntered = DateTime.Now;
                jobnote.DateAction = DateTime.Now;

                if (rd3.Checked == true)
                {
                    jobnote.StatusJobNote = "NAR";
                }
                else if (rd4.Checked == true)
                {
                    jobnote.StatusJobNote = "To Be Actioned";
                    jobnote.StatusJob = 0;

                }
                else if (rd5.Checked == true)
                {
                    jobnote.StatusJobNote = "Has been Actioned";
                    jobnote.StatusJob = 1;
                    jobDAO.Update_JobStaus(Convert.ToInt32(id));
                }

                if (rd1.Checked == true)
                {
                    jobnote.NoteType = "Requires Action";
                    jobDAO.InsUp_JobNote(jobnote, 1);
                    #region StatusJob
                        TblJobStatus JobStatus = new TblJobStatus();
                        JobStatus.JobID = Convert.ToInt32(id);
                        JobStatus.StatusDate = Convert.ToDateTime(DateTime.Now);
                        JobStatus.StatusJobs = "Finish";
                        JobStatus.Notes = txtNote.Text;
                        StatusJobDAO.JobStatusIns(JobStatus);
                    #endregion
                }
                else if (rd2.Checked == true)
                {
                    jobnote.NoteType = "No Action Requires";
                    jobDAO.InsUp_JobNote(jobnote, 1);

                    #region StatusJob
                        TblJobStatus JobStatus = new TblJobStatus();
                        JobStatus.JobID = Convert.ToInt32(id);
                        JobStatus.StatusDate = Convert.ToDateTime(DateTime.Now);
                        JobStatus.StatusJobs = "New Update";
                        JobStatus.Notes = txtNote.Text;
                        StatusJobDAO.JobStatusIns(JobStatus);
                    #endregion
                }
                flag = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            if (flag)
            {
                loadJobNotes();
            }
        }

        // Get IDJob
        private void GetIDJob()
        {
            string idJob = "";
            DataTable dt = jobDAO.GetJobNoteID(txtJobNoteId.Text);
            foreach (DataRow dr in dt.Rows)
            {
                idJob = dr["JobID"].ToString();
            }
            txtJobId.Text = idJob;
        }

        private void ActionList_Load(object sender, EventArgs e)
        {
            this.CenterToParent();

            if (txtJobNoteId.Text != "")
            {
                GetIDJob();
            }
         
        }

        private void rd1_CheckedChanged(object sender, EventArgs e)
        {
            txtNoteType.Text = rd1.Text;
        }

        private void rd2_CheckedChanged(object sender, EventArgs e)
        {
            txtNoteType.Text = rd2.Text;
        }

        private void rd3_CheckedChanged(object sender, EventArgs e)
        {
            txtStatus.Text = rd3.Text;
        }

        private void rd4_CheckedChanged(object sender, EventArgs e)
        {
            txtStatus.Text = rd4.Text;
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            txtStatus.Text = rd5.Text;
        }

        private void loadJobNotes()
        {
            try
            {
                int id = Global.ConvertToInt(txtJobId.Text);
                grdActionList.DataSource = jobDAO.GetJobNotes(id);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
